import server from '@/config/server/index.js'
import dictMgr from '@/biz/dict.js'
import dt from '@dt/dt'

let api = {
  getWebOrg: () => {
    return server.api().get('/hft-portal/org/getAuthOrgTree')
  },
  getInsOrg: server.api().get('/hft-portal/org/getInsOrg'),
  getOrgList: server.api().get('/hft-portal/org/getOrg'),
  getParentOrg: server.api().get('/hft-portal/org/getParentOrg') //根据orgId获取其所有的父组织id
}

// 根据权限获取组织架构
function getOrganization(data) {
  let params = {
    orgId: data?.filterOrgId || ''
  }
  if (data?.isShowProject) {
    params.isShowProject = true
    params.showAllProject = true
  }
  if (data?.isShowHouse) {
    params.isShowHouse = true
    params.showAllHouse = true
  }
  return api.getOrgList.fetch(params).then((res) => {
    if (!data?.filterOrgId) {
      if (res.length > 0) res[0].selected = true
    }
    return dealOrganization(res, data?.level || 3, data?.expand || false)
  })
}

function getInsOrganization(level = 3, expand = false) {
  return api.getInsOrg.fetch().then((res) => {
    if (res.length > 0) {
      res[0].selected = true
    }
    return res.map((org) => ({
      label: org.orgName,
      value: org.id,
      title: org.orgName,
      orgId: org.id,
      orgCode: org.orgCode,
      id: org.id,
      selected: org.selected,
      level: org.level,
      orgName: org.orgName,
      expand: (org.level == 1 ? true : false) || expand,
      children:
        level - 1 > 0
          ? org.ins.map((ins) => ({
              label: ins.insName,
              value: ins.insId,
              title: ins.insName,
              orgId: org.id,
              orgCode: org.orgCode,
              id: ins.id,
              level: ins.level,
              orgName: ins.orgName,
              insId: ins.insId,
              isBlind: ins.insId ? null : 1,
              children:
                ins.regions?.map((reg) => ({
                  label: reg.insName,
                  value: reg.insId,
                  title: reg.insName,
                  insId: ins.insId,
                  orgId: org.id,
                  orgCode: org.orgCode,
                  id: reg.id,
                  level: reg.level,
                  orgName: reg.orgName,
                  regionId: reg.insId
                })) ?? []
            }))
          : null
    }))
  })
}

function dealOrganization(res, level, expand) {
  return res.map((i) => {
    i.label = i.orgName
    i.value = i.id
    i.title = i.orgName
    i.expand = (i.level == 1 ? true : false) || expand
    if (level - 1 > 0) {
      i.children = dealOrganization(i.children, level - 1, expand)
    } else {
      delete i.children
    }
    return i
  })
}

// 权限树
function getOrgList(info) {
  return api.getOrgList.fetch(info).then((res) => {
    expandFun(res)
    return res
  })
}

function expandFun(data) {
  data.forEach((item) => {
    item.expand = 'true'
    if (item.children && item.children.length) {
      expandFun(item.children)
    }
  })
}

function getParentOrg(info) {
  return api.getParentOrg.fetch(info).then((res) => {
    return res
  })
}

export default {
  getOrganization,
  getInsOrganization,
  getOrgList,
  getParentOrg
}
